package cn.tedu.coolshark.controller;

import cn.tedu.coolshark.entity.User;
import cn.tedu.coolshark.mapper.Usermapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @Classname: UserController
 * @Author: bromide
 * @CreateTime: 2022/5/17--11:43
 * @Version: V1.0
 * @Description:
 */
@RestController
public class UserController {
    @Autowired(required = false)
    Usermapper mapper;

    @RequestMapping("/login")
    public int login(@RequestBody User user, HttpSession session){
        User u = mapper.select(user.getUsername());
        if (u!=null){
            if (u.getPassword().equals(user.getPassword())){
                //把当前登录成功的对象保存到会话对象中
                session.setAttribute("user",u);
                return 1;
            }
        }
        return 2;
    }

    @RequestMapping("/currentUser")
    public User currentUser(HttpSession session){
        User u = (User)session.getAttribute("user");
        return u;
    }
    @RequestMapping("/logout")
    public void logout(HttpSession session){
        //把登录成功时保存的user对象删除
        session.removeAttribute("user");
    }
}
